package com.jy.modules.cims.component.balance.factory;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.component.balance.dto.FeeBuildDto;
import com.jy.modules.cims.component.balance.dto.FeeParamDto;
import com.jy.modules.cims.component.balance.servicefee.ServiceFeeI;
import com.jy.modules.cims.component.balance.servicefee.impl.Default;
import com.jy.modules.cims.component.balance.servicefee.impl.ED;
import com.jy.modules.cims.component.balance.servicefee.impl.TBJ;
import com.jy.modules.cims.component.balance.servicefee.impl.WC;
import com.jy.modules.cims.data.common.dao.TCMmCostParamDao;
import com.jy.modules.cims.data.common.dao.TCMmRaisePlanDao;
import com.jy.modules.cims.data.common.dto.TCMmCostParamDTO;
import com.jy.modules.cims.data.common.dto.TCMmCostPlanDTO;
import com.jy.modules.cims.data.common.dto.TCMmRaisePlanDTO;
import com.jy.modules.cims.data.dao.CostPlanDao;
import com.jy.modules.cims.data.dto.CreditDetailDto;

@Service("com.jy.modules.cims.component.balance.factory.ParamFactory")
public class ParamFactory implements ParamFactioryI{

	@Autowired
	private CostPlanDao costPlanDao;
	
	@Autowired
	private TCMmCostParamDao tcmmCostParamDao;

	@Autowired
	private TCMmRaisePlanDao tcmmRaisePlanDao;
	
	/**
	 * 构造参数方法
	 * @author zhangmi
	 * @date 2016-8-16 下午4:57:31
	 * @see com.jy.modules.cims.component.balance.factory.ParamFactioryI#build(com.jy.modules.cims.component.balance.dto.FeeParamDto)
	 */
	@Override
	public FeeBuildDto build(FeeParamDto feeParamDto) throws Exception{
		
		
		String cooperorgCode = feeParamDto.getCooperorgCode();
		String costCode = feeParamDto.getCostCode(); //费用编码
		String dealType = feeParamDto.getDealType(); //费用计算类型
		TCMmCostParamDTO tcmmCostParamDto = null;
		if(!StringUtils.hasText(cooperorgCode)){
			throw new Exception("合作机构编号为空！");
		}
		if(!StringUtils.hasText(costCode)){
			throw new Exception("费用编码为空！");
		}
		
		FeeBuildDto feeBuildDto = new FeeBuildDto();
		ServiceFeeI serviceFee = null;
		//初始化每个机构对应的接口实例
		if("ED".equals(cooperorgCode)){
			serviceFee = (ServiceFeeI) new ED();
		}if("WC".equals(cooperorgCode)){
			serviceFee = (ServiceFeeI) new WC();
		}if("TBJ".equals(cooperorgCode)){
			serviceFee = (ServiceFeeI) new TBJ();
		} else{
			serviceFee = (ServiceFeeI) new Default();
		}
		//根据参数编码，获取对应的配置
		TCMmCostParamDTO costParamdto = new TCMmCostParamDTO();
		costParamdto.setCostCode(costCode);
		List<TCMmCostParamDTO> list = tcmmCostParamDao.searchTCMmCostParam(SearchParamUtil.getSearchParam(costParamdto));
		if(CollectionsUtils.isNotEmpty(list)){
			tcmmCostParamDto = list.get(0);
		}else{
			throw new Exception("根据费用编码"+costCode+"查询的数据为空！");
		}
		
			
		/**
		 * 贷款
		 */
		if(CimsConstant.DEAL_TYPE_LOAN_OUT.equals(dealType)){
			if("TBJ".equals(cooperorgCode)){
				//募集计划编号
				String planId = feeParamDto.getPlanId();
				if(StringUtils.hasText(planId)){
					TCMmRaisePlanDTO raisePlanDto = new TCMmRaisePlanDTO();
					raisePlanDto.setPlanId(planId);
					List<TCMmRaisePlanDTO> raiseList = tcmmRaisePlanDao.searchTCMmRaisePlan(SearchParamUtil.getSearchParam(raisePlanDto));
					if(!CollectionsUtils.isEmpty(raiseList)){
						feeBuildDto.setTcmmRaisePlanDto(raiseList.get(0));//募集计划信息
					}else{
						throw new Exception("查询募集编号"+planId+"数据为空！");
					}
				}else{
					throw new Exception("募集编号为空！");
				}
			}
			feeBuildDto.setLoanList(feeParamDto.getLoanList());
		}if(CimsConstant.DEAL_TYPE_CREDIT_OUT.equals(dealType)){ //债权
			//债权list
			List<CreditDetailDto> creditList = feeParamDto.getCreditList();//债权集合
			//如果担保费，风险金需要用剩余本金或者剩余本息。则计算该债权价值的剩余本金，剩余利息
			if(isSybjBondFee(tcmmCostParamDto.getGuaranteeFeeBase())
					||isSybjRiskFee(tcmmCostParamDto.getMarginBase())){
				TCMmCostPlanDTO dto = null;
				//计算剩余本金，剩余利息，剩余本息
				for (CreditDetailDto creditDetailDto : creditList) {
					dto = new TCMmCostPlanDTO();
					dto.setTransferId(creditDetailDto.getTransferId());
					TCMmCostPlanDTO tcmmCostPlanDTO = costPlanDao.getSumMustAmountAccrual(SearchParamUtil.getSearchParam(dto));
					if(tcmmCostPlanDTO!=null){
						creditDetailDto.setMustAmount(tcmmCostPlanDTO.getMustAmount());
						creditDetailDto.setMustAccrual(tcmmCostPlanDTO.getMustAccrual());
					}
				}
			}
			feeBuildDto.setCreditList(creditList);
		}else{
			feeBuildDto.setCreditList(feeParamDto.getCreditList());
		}
		
		//赋值
		feeBuildDto.setServiceFee(serviceFee);
		feeBuildDto.setTcmmCostParamDto(tcmmCostParamDto);
		return feeBuildDto;
	}
	
	
	/**
	 * 
	 * @Description 验证担保费是否，用剩余本金计算
	 * @author zhangmi
	 * @date 2016-4-8
	 */
	public boolean isSybjBondFee(String bondCode){
		if(CimsConstant.BASE_BONDFEE_THISCREDITVALUE.equals(bondCode)){//当前债权价值
			return false;
		}else if(CimsConstant.BASE_BONDFEE_SYBJ.equals(bondCode)){ //剩余本金
			return true;
		}else if(CimsConstant.BASE_BONDFEE_SYBX.equals(bondCode)){ //剩余本息 = 剩余本金+剩余本息
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @Description 验证风险金，是否用剩余本金或剩余本息，计算
	 * @param costCode
	 * @author zhangmi
	 * @date 2016-4-8
	 */
	public boolean isSybjRiskFee(String riskCode){
		if(CimsConstant.BASE_RISKFEE_HT.equals(riskCode)){
			return false;
		}else if(CimsConstant.BASE_RISKFEE_FK.equals(riskCode)){//当前债权价值
			return false;
		}else if(CimsConstant.BASE_RISKFEE_SYBJ.equals(riskCode)){ //剩余本金
			return true;
		}else if(CimsConstant.BASE_RISKFEE_SYBX.equals(riskCode)){ //剩余本息 = 剩余本金+剩余本息
			return true;
		}else if(CimsConstant.BASE_RISKFEE_JGCREDIT.equals(riskCode)){ //剩余本息 = 剩余本金+剩余本息
			return true;
		}
		return false;
	}
	
}
